<?php

/**
 * Form Select Element class
 *
 * @author Mathieu Pheulpin - sokaru91@gmail.com
 *
 */
class Mac_Form_Element_Select extends Mac_Form_Element_Abstract
{
    protected $emptyByDefault = false;

    protected $optGroups = array();

    protected $options = array();

    protected $multiple = false;

    protected $size = 1;

    public function setOptions (array $options, $optGroup = null)
    {
        if($optGroup === null) {
            $this->options = $options;
        } else {
            $this->optGroups[(string)$optGroup] = $options;
        }
        return $this;
    }

    public function addOption ($value, $name, $optGroup = null)
    {
        if($optGroup === null) {
            $this->options[$value] = $name;
        } else {
            $this->optGroups[(string)$optGroup][$value] = $name;
        }
        return $this;
    }

    public function setEmptyByDefault ($is = true)
    {
        $this->emptyByDefault = (boolean)$is;
        return $this;
    }

    public function setMultiple ($is = true)
    {
        $this->multiple = (boolean)$is;
        return $this;
    }

    public function setSize ($size)
    {
        $this->size = $size;
        return $this;
    }

    public function __toString ()
    {
        $select = '<select
            id="'.$this->id.'"
            name="'.$this->name.(($this->multiple === true) ? '[]' : '').'"
            size="'.$this->size.'"
            '.((count($this->class) > 0) ? 'class="'.implode(' ', $this->class).'"' : '').'
            '.(($this->multiple === true) ? 'multiple="multiple"' : '').'
            '.$this->printOptions().'
        >';
        if ($this->emptyByDefault === true) {
            $select .= $this->printOption('', '');
        }
        foreach($this->options as $value => $name) {
            $select .= $this->printOption($name, $value);
        }
        foreach($this->optGroups as $label => $options) {
            $select .= '<optgroup label="'.$label.'">';
            foreach($options as $value => $name) {
                $select .= $this->printOption($name, $value);
            }
            $select .= '</optgroup>';
        }
        $select .= '</select>';
        return $select;
    }

    protected function printOption ($name, $value)
    {
        if(is_array($this->value)) {
            // If multipe selection, value is an array
            $is_selected = (($value !== '') && in_array($value, $this->value));
        } else {
            // We don't select the empty field
            $is_selected = (($value !== '') && ($value===$this->value));
        }
        return '<option value="'.$value.'"'.(($is_selected===true) ? ' selected="selected"' : '').'>'.$name.'</option>';
    }

    public function isValid ($data)
    {
        if(is_array($this->value)) {
            $emptyKey = array_search('', $this->value);
            if ($emptyKey !== false) {
                unset($this->value[$emptyKey]);
            }
            if(count($this->value) === 0) {
                return false;
            }
        }
        return parent::isValid($data);
    }
    
}